<?php


namespace App\Http\Controllers;


use App\Model\Category;
use App\Model\Goods;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CollectionController extends Controller
{

    public function cat($cat)
    {
        $cat = intval($cat);
        $cat = Category::find($cat);
        if (empty($cat)) {
            throw new NotFoundHttpException();
        }

        $goods = Goods::getGoodsByCat($cat->id);

        return view('home.cat', ['goods' => $goods ]);
    }

    public function search()
    {
        $query = request()->input('q');

        if (empty($query)) {
            return redirect('/');
        }

        $goods = Goods::findGoods()->where('goods_name', 'like', '%'. $query .'%')->get();

        $title = $query;

        return view('home.search', ['goods' => $goods, 'title' => $title]);
    }

    public function all()
    {
        $query = 'All CATEGORIES';

        $goods = Goods::findGoods()->get();

        return view('home.search', ['goods' => $goods, 'title' => $query]);
    }


}
